home *** CD-ROM | disk | FTP | other *** search
/ Alde ADA 1: #1 / CCCC 8804 Volume 1 Number 1 - Alde.iso / C / MISC / FUNC / PROFF.ARC / PROFF01.C < prev    next >
Encoding:
C/C++ Source or Header  |  1988-02-21  |  13.7 KB  |  765 lines

  1. #include <stdio.h>
  2. #include <ctype.h>
  3. #include "proff.h"
  4. #include "debug.h"
  5.  
  6. /*
  7.  * bold - bold face or overstrike a line
  8.  *
  9.  */
  10. bold(buf, tbuf, size)
  11. char buf[];
  12. char tbuf[];
  13. int size;
  14. {
  15.    int i, j;
  16.  
  17.    dprintf("bold  ");
  18.  
  19.    j = 0;
  20.    for (i = 0; buf[i] != '\n' && j < size - 2; i++)
  21.    {
  22.       tbuf[j] = buf[i];
  23.       j++;
  24.       if (buf[i] != ' ' && buf[i] != '\t' &&
  25.           buf[i] != BACKSPACE)
  26.       {
  27.          tbuf[j] = BACKSPACE;
  28.          tbuf[j + 1] = tbuf[j - 1];
  29.          j += 2;
  30.       }
  31.    }
  32.    tbuf[j] = '\n';
  33.    tbuf[j + 1] = '\0';
  34.    strcpy(buf, tbuf);
  35. }
  36.  
  37. /*
  38.  * brk - end current filled line
  39.  *
  40.  */
  41. brk()
  42. {
  43.    dprintf("brk  ");
  44.    if (outp > 0)
  45.    {
  46.       outbuf[outp] = '\n';
  47.       outbuf[outp + 1] = EOS;
  48.       put(outbuf);
  49.    }
  50.    outp = 0;
  51.    outw = 0;
  52.    outwds = 0;
  53. }
  54.  
  55. /*
  56.  * center - center a line by setting tival
  57.  *
  58.  */
  59. center(buf)
  60. char buf[];
  61. {
  62.    int i;
  63.  
  64.    dprintf("center  ");
  65.  
  66.    i = (rmval + tival - width(buf)) / 2;
  67.    tival = (i > 0) ? i : 0;
  68. }
  69.  
  70. /*
  71.  * doroff - format text in file fp
  72.  *
  73.  */
  74. doroff(fp)
  75. FILE *fp;
  76. {
  77.    char inbuf[INSIZE];
  78.  
  79.    infile[0] = fp;
  80.    for (level = 0; level > -1; level--)
  81.    {
  82.       while (ngetln(inbuf, infile[level]) != EOF)
  83.       {
  84.          if (inbuf[0] == cchar)        /* a command */
  85.             command(inbuf);
  86.          else
  87.          {
  88. #ifdef rainbow
  89.             if (biosb(2))
  90.                exit(0);
  91. #endif
  92.             text(inbuf);
  93.             p_txtlines++;
  94.          }
  95.       }
  96.       if (level > 0 && infile[level] > 0)
  97.       {
  98.          fclose(infile[level]);
  99.          if (verbose)
  100.             fprintf(stderr, "       done.\n");
  101.       }
  102.    }
  103. }
  104.  
  105. /*
  106.  * gettl - copy title from buf to ttl
  107.  *
  108.  * modifies lim
  109.  */
  110. gettl(buf, ttl, lim)
  111. char *buf;
  112. char *ttl;
  113. int lim[];
  114. {
  115.    while (!isspace(*buf))
  116.       buf++;
  117.    while (isspace(*buf))
  118.       buf++;
  119.    strcpy(ttl, buf);
  120.    lim[0] = inval;
  121.    lim[1] = rmval;
  122. }
  123.  
  124. /*
  125.  * getwrb - get a word INCLUDING the trailing blanks
  126.  *
  127.  */
  128. int getwrb(in, i, out)
  129. char in[];
  130. char out[];
  131. int *i;
  132. {
  133.    int j, k;
  134.  
  135.    dprintf("getval  ");
  136.    k = *i;
  137.    j = 0;
  138.    while (in[k] != EOS && in[k] != ' ' && in[k] != '\t' && in[k] != '\n')
  139.    {
  140.       out[j] = in[k];
  141.       k++;
  142.       j++;
  143.    }
  144.    while (in[k] == ' ')
  145.    {
  146.       out[j] = ' ';
  147.       k++;
  148.       j++;
  149.    }
  150.    *i = k;
  151.    out[j] = EOS;
  152.    return (j);
  153. }
  154.  
  155. /*
  156.  * gfield - get next tab or title field
  157.  *
  158.  */
  159. int gfield(buf, i, n, temp, delim)
  160. char buf[];
  161. int *i;
  162. int n;
  163. char temp[];
  164. char delim;
  165. {
  166.    int j, k;
  167.  
  168.    dprintf("gfield  ");
  169.    j = 0;
  170.    k = *i;
  171.    if (n > 0)
  172.    {
  173.       if (buf[k] == delim)
  174.          k++;
  175.       while (buf[k] != delim && buf[k] != EOS && buf[k] != '\n' && j <= n)
  176.       {
  177.          temp[j] = buf[k];
  178.          j++;
  179.          k++;
  180.       }
  181.    }
  182.    temp[j] = EOS;
  183.    while (buf[k] != delim && buf[k] != EOS && buf[k] != '\n')
  184.       k++;
  185.    *i = k;
  186.    return (j);
  187. }
  188.  
  189. /*
  190.  * jcopy - scopy without copying EOS
  191.  */
  192. jcopy(from, i, to, j)
  193. char from[];
  194. char to[];
  195. int i;
  196. int j;
  197. {
  198.    int k1, k2;
  199.  
  200.    dprintf("jcopy  ");
  201.  
  202.    k1 = i;
  203.    k2 = j;
  204.    while (from[k1] != EOS)
  205.    {
  206.       to[k2] = from[k1];
  207.       k1++;
  208.       k2++;
  209.    }
  210. }
  211.  
  212. /*
  213.  * justfy - justifies string in its tab column
  214.  */
  215. justfy(in, left, right, type, out)
  216. char in[];
  217. char out[];
  218. int left;
  219. int right;
  220. int type;
  221. {
  222.    int j, k, n;
  223.  
  224.    dprintf("justfy  ");
  225.    n = width(in);
  226.    if (type == RIGHT)
  227.       jcopy(in, 0, out, right - n);
  228.    else
  229.    if (type == CENTER)
  230.    {
  231.       k = (right + left - n) / 2;
  232.       j = (k > left) ? k : left;
  233.       jcopy(in, 0, out, j);
  234.    }
  235.    else
  236.       jcopy(in, 0, out, left);
  237. }
  238.  
  239. /*
  240.  * leadbl - delete leading blanks, set tival
  241.  */
  242. leadbl(buf)
  243. char buf[];
  244. {
  245.    int i, j;
  246.  
  247.    dprintf("leadbl  ");
  248.    brk();
  249.    for (i = 0; buf[i] == ' '; i++)     /* find 1st non-blank */
  250.       ;
  251.    if (buf[i] != '\n')
  252.       if (autopar)
  253.       {
  254.          put("\n");                    /* blank line */
  255.          tival = inval + autoprv;
  256.       }
  257.       else
  258.          tival = inval + i;            /* ??????????? */
  259.    for (j = 0; buf[i] != EOS; j++)
  260.    {                                   /* move line to left */
  261.       buf[j] = buf[i];
  262.       i++;
  263.    }
  264.    buf[j] = EOS;
  265. }
  266.  
  267. /*
  268.  * ngetln - get next line from f into line
  269.  */
  270. int ngetln(line, f)
  271. char line[];
  272. FILE *f;
  273. {
  274.    int c, i;
  275.  
  276.    for (i = 0; (c = (bp >= 0) ? buf[bp--] : getc(f)) != EOF;)
  277.    {
  278.       if (i < MAXLINE - 1)
  279.       {
  280.          line[i++] = (char) c;
  281.       }
  282.       if (c == '\n' || c == '\r')
  283.          break;
  284.    }
  285.    line[i] = EOS;
  286.    if (i == 0 && c == EOF)
  287.       i = EOF;
  288. #ifdef DEBUG
  289.    printf("ngetln: %s (line)\n", line);
  290. #endif
  291.    return (i);
  292. }
  293.  
  294. /*
  295.  * pbstr - push string back onto input
  296.  *
  297.  */
  298. pbstr(in)
  299. char in[];
  300. {
  301.  
  302.    int i;
  303.  
  304.    dprintf("pbstr  ");
  305.    for (i = strlen(in) - 1; i >= 0; i--)
  306.       putbak(in[i]);
  307. }
  308.  
  309. /*
  310.  * pfoot - put out page footer
  311.  *
  312.  */
  313. pfoot()
  314. {
  315.    dprintf("pfoot  ");
  316.    skipl(m3val);
  317.    if (m4val > 0)
  318.    {
  319.       if (curpag % 2 == 1)
  320.          puttl(efoot, eflim, curpag);
  321.       else
  322.          puttl(ofoot, oflim, curpag);
  323.    }
  324.    if (print == YES)                   /* flush the page */
  325.    {
  326.       putchar(PAGEJECT);               /* ...            */
  327.       p_outpages++;
  328.       if (stopx > 0)                   /* -s, so flush ^L */
  329.          putchar('\n');
  330.    }
  331. }
  332.  
  333. /*
  334.  * phead - put out page header
  335.  *
  336.  */
  337. phead()
  338. {
  339.    dprintf("phead  ");
  340.  
  341.    curpag = newpag;
  342.    if (curpag >= frstpg && curpag <= lastpg)
  343.       print = YES;
  344.    else
  345.       print = NO;
  346.    if (stopx > 0 && print == YES)
  347.       prmpt(&stopx);
  348.    newpag++;
  349.    if (m1val > 0)
  350.    {
  351.       skipl(m1val - 1);
  352.       if (curpag % 2 == 0)
  353.          puttl(ehead, ehlim, curpag);
  354.       else
  355.          puttl(ohead, ohlim, curpag);
  356.    }
  357.    skipl(m2val);
  358.    lineno = m1val + m2val + 1;
  359. }
  360.  
  361. /*
  362.  * prmpt - pause for paper insertion
  363.  * prompt if i == 1; increment i
  364.  *
  365.  */
  366. prmpt(i)
  367. int *i;
  368. {
  369.    int junk, j;
  370.    static char bellst[2] = {BEL, EOS};
  371.  
  372.    dprintf("prmpt  ");
  373.    j = *i;
  374.    if (j == 1)
  375. #ifdef rainbow
  376.       printf("%s\033[7minsert paper and type return\033[0m ", bellst);
  377. #else
  378.       printf("%sinsert paper and type return ", bellst);
  379. #endif
  380.    else
  381.       printf(bellst);
  382.    junk = getchar();
  383.    *i = ++j;
  384. }
  385.  
  386. /*
  387.  * Put - put out line with proper spacing and indenting
  388.  *
  389.  */
  390. put(buf)
  391. char buf[];
  392. {
  393.    register int i;
  394.  
  395.    dprintf("put  ");
  396.    if (lineno == 0 || lineno > bottom)
  397.       phead();
  398.  
  399.    if (print == YES)
  400.    {
  401.       if (buf[0] == '\n')
  402.       {                                /* empty line.. */
  403.          putchar('\n');
  404.          p_outlines++;
  405.       }
  406.       else
  407.       {
  408.          for (i = 1; i <= offset; i++) /* page offset */
  409.             putchar(' ');
  410.          for (i = 1; i <= tival; i++)  /* indenting   */
  411.             putchar(' ');
  412.  
  413.          while (*buf != '\0')
  414.          {
  415.             putchar(*buf);
  416.             buf++;
  417.          }
  418.          p_outlines++;
  419.       }
  420.    }
  421.    tival = inval;
  422.    skipl(((lsval - 1 < bottom - lineno) ? lsval - 1 : bottom - lineno));
  423.    lineno += lsval;
  424.  
  425.    if (lineno > bottom)
  426.       pfoot();
  427. }
  428.  
  429. /*
  430.  * putbak - push character back onto input
  431.  *
  432.  */
  433. putbak(c)
  434. char c;
  435. {
  436.    dprintf("putbak  ");
  437.  
  438.    bp++;
  439.    if (bp > BUFSIZE)
  440.       error("too many characters pushed back.\n");
  441.    buf[bp] = c;
  442. }
  443.  
  444.  
  445. /*
  446.  * puttl - put out title line with optional page number & date
  447.  * 
  448.  */
  449. puttl(buf, lim, pageno)
  450. char buf[];
  451. int lim[];
  452. int pageno;
  453. {
  454.    char chars[9], cdate[27];
  455.    char rmstr[MAXTOK];
  456.    char delim;
  457.    char *tp;
  458.    int j;
  459.    int nc, n, i, left, right, ncd;
  460.  
  461.    dprintf("puttl  ");
  462.    if (print == NO)
  463.       return;
  464.    left = lim[0];                      /* no more +1 here */
  465.    right = lim[1];                     /* no more +1 here */
  466.    nc = itoc(pageno, chars, MAXCHARS);
  467.    if (roman)
  468.    {
  469.       nc = cvtroman(chars, rmstr);
  470.       strcpy(chars, rmstr);
  471.    }
  472.    getnow(cdate);
  473.    ncd = strlen(cdate);
  474.    i = 0;
  475.    delim = buf[i];
  476.    for (j = 0; j < right; j++)
  477.       ttl[j] = ' ';
  478.    n = 0;
  479.    do
  480.    {
  481.       if (gfield(buf, &i, right - left, tbuf1, delim) > 0)
  482.       {
  483.          subst(tbuf1, PAGENUM, tbuf2, chars, nc);
  484.          subst(tbuf2, CURRENTDATE, tbuf1, cdate, ncd);
  485.          justfy(tbuf1, left, right, tjust[n], ttl);
  486.       }
  487.       n++;                             /* update title counter */
  488.    }
  489.    while (buf[i] != EOS && buf[i] != '\n' && n != 3);
  490.  
  491.    for (; right >= 1; right--)
  492.       if (ttl[right - 1] != ' ')
  493.          break;
  494.    ttl[right] = '\n';
  495.    ttl[right + 1] = EOS;
  496.    for (i = 1; i <= offset; i++)
  497.       putchar(' ');                    /* offset */
  498.    tp = ttl;
  499.    while (*tp != '\0')
  500.    {
  501.       putchar(*tp);
  502.       tp++;
  503.    }
  504.    p_outlines++;
  505. }
  506.  
  507. /*
  508.  * set - set parameter and check range
  509.  *
  510.  */
  511. set(param, val, argtyp, defval, minval, maxval)
  512. int *param;
  513. int val;
  514. int argtyp;
  515. int defval;
  516. int minval;
  517. int maxval;
  518. {
  519.    int i;
  520.  
  521.    dprintf("set  ");
  522.    i = *param;
  523.    if (argtyp == '\n')                 /* defaulted */
  524.       i = defval;
  525.    else if (argtyp == '+')             /* relative + */
  526.       i += val;
  527.    else if (argtyp == '-')             /* relative - */
  528.       i -= val;
  529.    else                                /* absolute  */
  530.       i = val;
  531.    i = (i < maxval) ? i : maxval;      /* min       */
  532.    i = (i > minval) ? i : minval;      /* max       */
  533.    *param = i;
  534. }
  535.  
  536. /*
  537. * skipl - output  n  blank lines
  538. *
  539. */
  540. skipl(n)
  541. register int n;
  542. {
  543.    register int i;
  544.  
  545.    dprintf("skip  ");
  546.    if (print == YES)
  547.       for (i = 1; i <= n; i++)
  548.       {
  549.          putchar('\n');
  550.          p_outlines++;
  551.       }
  552. }
  553.  
  554. /*
  555.  * space - space  n  lines or to bottom of page
  556.  *
  557.  */
  558. space(n)
  559. int n;
  560. {
  561.  
  562.    dprintf("space  ");
  563.    brk();
  564.    if (lineno > bottom)
  565.       return;
  566.    if (lineno == 0)
  567.       phead();
  568.    skipl(((n < bottom + 1 - lineno) ? n : bottom + 1 - lineno));
  569.    lineno += n;
  570.    if (lineno > bottom)
  571.       pfoot();
  572. }
  573.  
  574. /*
  575.  * spread - spread words to justify right margin
  576.  *
  577.  */
  578. spread(buf, outp, nextra, outwds)
  579. char buf[];
  580. int outp;
  581. int nextra;
  582. int outwds;
  583. {
  584.    int dir = 0;
  585.    register int i, j;
  586.    int nb, ne, nholes;
  587.  
  588.    dprintf("spread  ");
  589.    if (nextra <= 0 || outwds <= 1)
  590.       return;
  591.    dir = 1 - dir;                      /* reverse previous direction */
  592.    ne = nextra;
  593.    nholes = outwds - 1;
  594.    if (tival != inval && nholes > 1)
  595.       nholes--;
  596.    i = outp - 1;
  597.    j = (MAXOUT - 2 < i + ne) ? MAXOUT - 2 : i + ne;     /* leave room for '\n', EOS */
  598.    while (i < j)
  599.    {
  600.       buf[j] = buf[i];
  601.       if (buf[i] == ' ' && buf[i - 1] != ' ')
  602.       {
  603.          if (dir == 0)
  604.             nb = (ne - 1) / nholes + 1;
  605.          else
  606.             nb = ne / nholes;
  607.          ne -= nb;
  608.          nholes--;
  609.          for (; nb > 0; nb--)
  610.          {
  611.             j--;
  612.             buf[j] = ' ';
  613.          }
  614.       }
  615.       i--;
  616.       j--;
  617.    }
  618. }
  619.  
  620. /*
  621.  * subst - substitutes a string for a specified character
  622.  *
  623.  */
  624. subst(in, chr, out, subara, n)
  625. char in[];
  626. char chr;
  627. char out[];
  628. char subara[];
  629. int n;
  630. {
  631.    register int i, j, k;
  632.  
  633.    dprintf("subst  ");
  634.    j = 0;
  635.    for (i = 0; in[i] != EOS; i++)
  636.       if (in[i] == chr)
  637.          for (k = 0; k < n; k++)
  638.          {
  639.             out[j] = subara[k];
  640.             j++;
  641.          }
  642.       else
  643.       {
  644.          out[j] = in[i];
  645.          j++;
  646.       }
  647.    out[j] = EOS;
  648. }
  649.  
  650. /*
  651.  * Text process text lines
  652.  *
  653.  */
  654. text(inbuf)
  655. char inbuf[];
  656. {
  657.    int i;
  658.    register int j;
  659.    char wrdbuf[INSIZE];
  660.  
  661.    dovar(wrdbuf, inbuf);               /* expand variables */
  662.    strcpy(inbuf, wrdbuf);
  663.    doesc(inbuf, wrdbuf, INSIZE);       /* expand escapes   */
  664.    dotabs(inbuf, wrdbuf, INSIZE);      /* expand tabs      */
  665.  
  666.    if (inbuf[0] == ' ' || inbuf[0] == '\n')
  667.       leadbl(inbuf);                   /* move left, set tival */
  668.    if (ulval > 0 || ULon)              /* word underlining */
  669.    {
  670.       underl(inbuf, wrdbuf, INSIZE);
  671.       ulval--;
  672.    }
  673.    if (boval > 0 || BDon)              /* boldfacing */
  674.    {
  675.       bold(inbuf, wrdbuf, INSIZE);
  676.       boval--;
  677.    }
  678.    if (ceval > 0 || CEon)              /* centering */
  679.    {
  680.       center(inbuf);
  681.       put(inbuf);
  682.       ceval--;
  683.    }
  684.    else if (inbuf[0] == '\n')          /* all blank line */
  685.       put(inbuf);
  686.    else if (fill == NO)                /* unfilled text */
  687.       put(inbuf);
  688.    else                                /* filled text */
  689.    {
  690.       i = strlen(inbuf) - 1;
  691.       inbuf[i] = ' ';
  692.       if (inbuf[i - 1] == '.')
  693.       {
  694.          i++;
  695.          inbuf[i] = ' ';
  696.       }
  697.       inbuf[i + 1] = EOS;
  698.       for (i = 0; getwrb(inbuf, &i, wrdbuf) > 0;)
  699.          putwrd(wrdbuf);
  700.    }
  701. }
  702.  
  703. /*
  704.  * Underl       underline words in a line
  705.  *
  706.  */
  707. underl(buf, tbuf, size)
  708. char buf[];
  709. char tbuf[];
  710. int size;
  711. {
  712.    int i, j;
  713.  
  714.    j = 0;
  715.    for (i = 0; buf[i] != '\n' && j < size - 2; i++)
  716.    {
  717.       if (buf[i] != ' ' && buf[i] != BACKSPACE && buf[i] != '_')
  718.       {
  719.          tbuf[j++] = '_';
  720.          tbuf[j++] = BACKSPACE;
  721.       }
  722.       if (buf[i] == BLANK)
  723.          tbuf[j++] = ulblnk;
  724.       else
  725.          tbuf[j++] = buf[i];
  726.    }
  727.  
  728.    tbuf[j] = '\n';
  729.    tbuf[j + 1] = '\0';
  730.    strcpy(buf, tbuf);
  731. }
  732.  
  733. /*
  734.  * width - compute width of character string
  735.  *
  736.  */
  737. int width(buf)
  738. char buf[];
  739. {
  740.    int k, i;
  741.  
  742.    dprintf("width  ");
  743.    k = 0;
  744.    for (i = 0; buf[i] != EOS; i++)
  745.       if (buf[i] == BACKSPACE)
  746.          k--;
  747.       else
  748.       if (buf[i] >= ' ' && buf[i] <= '~')
  749.          k++;
  750.    return (k);
  751. }
  752.  
  753. /*
  754.  * getnow - get the date from command line if present.
  755.  *          if not specified, prompt user for it.
  756.  *
  757.  * (stub)
  758.  */
  759. getnow(date)
  760. char date[];
  761. {
  762.    dprintf("getnow  ");
  763.    strcpy(date, "00-xxx-1900 00:00:00");
  764. }
  765.